package pl.ptemich.tankwars.objects;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public class Board {

  private static final int BOARD_SIZE = 10;
  
  private static final int FIELD_SIZE = 20;
  
  private static final int FIELD_LINE_WIDTH = 1;
  
  private int boardPositionX = 100;
  
  private int boardPositionY = 100;
  
  private String[] rowNames;
  
  private String[] colNames;
  
  private int activeRow = 5;
  
  private int activeColumn = 6;
  
  public Board(int boardPositionX, int boardPositionY) {
    this.boardPositionX = boardPositionX;
    this.boardPositionY = boardPositionY;
    
    rowNames = new String[BOARD_SIZE];
    char name = 'A';
    for (int index = 0; index < BOARD_SIZE; index++) {
      rowNames[index] = String.valueOf(name);
      name++;
    }
    
    colNames = new String[BOARD_SIZE];
    int nameNumber = 1;
    for (int index = 0; index < BOARD_SIZE; index++) {
      colNames[index] = String.valueOf(nameNumber);
      nameNumber++;
    }    
  }
  
  public void render(Graphics graphics) throws SlickException {
    graphics.setLineWidth(FIELD_LINE_WIDTH);

    for (int rowIndex = 0; rowIndex < BOARD_SIZE; rowIndex++) {
      // draw row name
      graphics.setColor(Color.white);
      graphics.drawString(rowNames[rowIndex], 
                          boardPositionX - FIELD_SIZE, 
                          boardPositionY + rowIndex * FIELD_SIZE + FIELD_LINE_WIDTH);
      if (rowIndex == 0) {
        for (int columnIndex = 0; columnIndex < BOARD_SIZE; columnIndex++) {
          graphics.setColor(Color.white);
          graphics.drawString(colNames[columnIndex], 
              boardPositionX + columnIndex * FIELD_SIZE, 
              boardPositionY - FIELD_SIZE);
        }
      }
      for (int columnIndex = 0; columnIndex < BOARD_SIZE; columnIndex++) {
        if (activeRow == rowIndex && activeColumn == columnIndex) {
          graphics.setColor(Color.black); 
        } else {
          graphics.setColor(Color.blue);
        }
        graphics.fillRect(boardPositionX + columnIndex * FIELD_SIZE + FIELD_LINE_WIDTH, 
                          boardPositionY + rowIndex * FIELD_SIZE + FIELD_LINE_WIDTH, 
                          FIELD_SIZE - FIELD_LINE_WIDTH,  FIELD_SIZE - FIELD_LINE_WIDTH);        
      }
    }
  }  
  
  
  public int getBoardWidth() {
    return BOARD_SIZE * FIELD_SIZE;
  }
  
  public int getBoardHeight() {
    return BOARD_SIZE * FIELD_SIZE;
  }
  
}
